package example.tallydemo.MyEntity;

import android.graphics.Color;

import com.github.mikephil.charting.charts.PieChart;
import com.github.mikephil.charting.components.Description;
import com.github.mikephil.charting.data.PieData;
import com.github.mikephil.charting.data.PieDataSet;
import com.github.mikephil.charting.data.PieEntry;
import com.github.mikephil.charting.formatter.PercentFormatter;
import com.github.mikephil.charting.utils.ColorTemplate;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by tuzha on 2018/4/6.
 */

public class MyPieChart
{
    PieChart mPieChart;
    PieDataSet dataSet;
    PieData pieData;
    Description description;
    List<AccountBook> books;
    List<PieEntry> strings;
    float totalMoney;


    public MyPieChart()
    {
        strings = new ArrayList<>(); //设置数据
    }

    public MyPieChart(PieChart mPieChart, List<AccountBook> books)
    {
        this.mPieChart = mPieChart;
        this.books = books;
        strings = new ArrayList<>(); //设置数据
    }

    public void CountTotalMoney()
    {
        for (AccountBook it : books)
        {
            totalMoney += it.getMoney();
        }
    }

    public void setData(String str)
    {
        float percent;
        CountTotalMoney();
        for (AccountBook it: books)
        {
            percent = (float)(it.getMoney() / totalMoney);
            if (percent > 0.1)
            {
                strings.add(new PieEntry(percent * 100,it.getClassify()));
            }
            else
            {
                strings.add(new PieEntry(percent * 100,null));
            }
        }
        dataSet = new PieDataSet(strings,str); //把数据添加到PieDataSet里面
    }


    public void addBook(AccountBook it,String str)
    {
        books.add(it);
        totalMoney = 0;
        strings.clear();
        setData(str);
    }

    public void clearPieChart()
    {
        this.books.clear();
        this.strings.clear();
        this.dataSet = null;
    }

    public void setTextAttribute()
    {
        //设置 文字显示在 外面
        dataSet.setValueLinePart1OffsetPercentage(80f);
        dataSet.setValueLinePart1Length(1.0f);
        dataSet.setValueLinePart2Length(0.9f);
        dataSet.setYValuePosition(PieDataSet.ValuePosition.OUTSIDE_SLICE);
        dataSet.setColors(ColorTemplate.COLORFUL_COLORS);
    }

    public void setDataAttribute()
    {
        pieData = new PieData(dataSet); //把PieDataSet 添加到 PieData里面
        pieData.setValueTextColor(Color.GREEN); //设置 value 的颜色
        pieData.setDrawValues(true); //设置value 的显示为真
        pieData.setValueFormatter(new PercentFormatter()); //设置value显示格式
        pieData.setValueTextSize(12f); //设置value 字体大小
    }

    public void setCaptions()
    {
        description = new Description(); //设置右下角说明文字
        description.setText("数据分析");
        description.setTextColor(Color.BLUE); //设置说明文字颜色
        description.setTextSize(12f); //设置说明文字大小
        mPieChart.setDescription(description);
    }

    public void setPieChartAttribute()
    {
        mPieChart.setHoleRadius(0f); //先设成半径为零
        mPieChart.setTransparentCircleRadius(0f); //发现内圈还有个半透明的，再设置半透明的半径为0
        mPieChart.setData(pieData);
    }

    public void update()
    {
        mPieChart.notifyDataSetChanged();
        mPieChart.invalidate();
    }

    public void initPieChart()
    {
        setTextAttribute();
        setDataAttribute();
        setCaptions();
        setPieChartAttribute();
    }
}
